package com.util;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

import com.jabistudio.androidjhlabs.filter.ContrastFilter;
import com.jabistudio.androidjhlabs.filter.DoGFilter;
import com.jabistudio.androidjhlabs.filter.ReduceNoiseFilter;
import com.jabistudio.androidjhlabs.filter.util.AndroidUtils;
import com.ocr.ImageActivity;

public class ProcessImgUtil {
	/*
	 * xu ly anh
	 */
	public Bitmap processImg(Bitmap bitmap) {
		Log.d("----OCR dang xu ly anh chat luong thap", "thoainguyen");
		try {
			bitmap = bitmap.copy(
					Bitmap.Config.ARGB_8888, true);
			int width = bitmap.getWidth();
			int height = bitmap.getHeight();
			int[] src;
			int[] result;
			/*
			 * remove noise
			 */
			ReduceNoiseFilter reduceNoiseFilter = new ReduceNoiseFilter();
			src = AndroidUtils.bitmapToIntArray(bitmap);
			result = reduceNoiseFilter.filter(src, width, height);
			bitmap = Bitmap.createBitmap(result, width, height,
					Config.ARGB_8888);
			/*
			 * adjust if too dark
			 */

			// increase brightness and reduce contrast
			ContrastFilter filter = new ContrastFilter();
			filter.setContrast(0.8f);
			filter.setBrightness(1.2f);
			// Change int Array into a bitmap
			src = AndroidUtils.bitmapToIntArray(bitmap);
			// Applies a filter.
			result = filter.filter(src, width, height);
			// Change the Bitmap int Array (Supports only ARGB_8888)
			bitmap = Bitmap.createBitmap(result, width, height,
					Config.ARGB_8888);

			// dog fillter
			src = AndroidUtils.bitmapToIntArray(bitmap);
			DoGFilter doGFilter = new DoGFilter();
			// doGFilter.setRadius1(doGFilter.getRadius1()-5);
			// doGFilter.setRadius2(doGFilter.getRadius2()-5);
			doGFilter.setNormalize(true);
			doGFilter.setInvert(true);
			result = doGFilter.filter(src, width, height);
			bitmap = Bitmap.createBitmap(result, width, height,
					Config.ARGB_8888);
			return bitmap;
		} catch (Exception ex) {
			return bitmap;
		}
	}
}
